package com.hyg.lib_music.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;


import com.hyg.lib_common.DataModel.Music.FavMusic;
import com.hyg.lib_common.DataModel.Music.Music;
import com.hyg.lib_common.DataModel.Music.MusicCategoryDBData;
import com.hyg.lib_common.DataModel.Music.MusicCategoryData;

import org.litepal.LitePal;

import java.util.List;


public class MusicFavManager {

    private static final MusicFavManager manager = new MusicFavManager();
    private static Context mContext;

    public static MusicFavManager sharedManager(Context context) {
        if (context != null) {
            MusicFavManager.mContext = context;
        }
        return manager;
    }

    public static MusicFavManager sharedManager() {
        return manager;
    }

//    //同步本地收藏
    public void syncFavorite(List<FavMusic> musicList) {
        LitePal.deleteAll(FavMusic.class);
        int i = 0;
        if (musicList != null){
            for (FavMusic music : musicList) {
                FavMusic fav = new FavMusic(music.musicId,music.musicName,music.musician,music.musicDuration,music.region,music.musicUrl,music.cateId,music.cateName);
                fav.saveTime = i;
                fav.save();
                i++;
            }
        }
    }

//    //添加本地收藏
    public void addFavorite(Music music) {
//        this.syncFavorite(null);
        FavMusic fav = new FavMusic(music.id,music.musicName,music.musician,music.musicDuration,music.region,music.musicUrl,music.cateId,music.cateName);
        fav.save();
//                saveOrUpdate("id=?", music.id+"");
    }

    //删除本地收藏
    public void removeFavorite(int musicId) {
        List<FavMusic> list = LitePal.findAll(FavMusic.class);
        for (FavMusic fav : list) {
            if (musicId == fav.musicId){
                fav.delete();
                break;
            }
        }
    }

    //是否在本地收藏
    public boolean isExist(int musicId) {

        List<FavMusic> list = LitePal.findAll(FavMusic.class);
        for (FavMusic fav : list) {
            if (musicId == fav.musicId){
                return true;
            }
        }
        return false;
    }

    //是否在本地收藏
    public List<FavMusic> getAll() {
        List<FavMusic> list = LitePal.findAll(FavMusic.class);
        return list;
    }

    //同步音乐类别
    public void syncMusicCategory(List<MusicCategoryData> categoryList) {
//        LitePal.deleteAll(MusicCategoryDBData.class);

        if (categoryList != null){
            for (MusicCategoryData musicCategoryData : categoryList) {
                addMusicCategoryDBData(musicCategoryData);
                for (MusicCategoryData sub : musicCategoryData.subList) {
                    addMusicCategoryDBData(sub);
                    for (MusicCategoryData item : sub.subList) {
                        addMusicCategoryDBData(item);
                    }
                }
            }
        }
    }

   private void addMusicCategoryDBData(MusicCategoryData data) {
        MusicCategoryDBData item = new MusicCategoryDBData();
        item.categoryId = data.id;
        item.cateName = data.cateName;
        item.logoUrl = data.logoUrl;
        item.save();
    }

    //获取音乐类别图标
    public String getLogoUrl(int cateId) {
        List<MusicCategoryDBData> list = LitePal.findAll(MusicCategoryDBData.class);
        for (MusicCategoryDBData item : list) {
            if (cateId == item.categoryId){
                return item.logoUrl;
            }
        }
        return "";
    }


    // 获取是否允许4G
    public boolean getAllowMobile(){

        SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(mContext);
        return sp.getBoolean("play_mobile", false);
    }
    // 设置是否允许4G
    public void setAllowMobile(boolean isAllow){
        SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(mContext);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("play_mobile", isAllow);
        editor.commit();
    }
}
